package cn.ren.controller;

import cn.ren.constant.MessageConstant;
import cn.ren.entity.PageResult;
import cn.ren.entity.QueryPageBean;
import cn.ren.entity.Result;
import cn.ren.pojo.*;
import cn.ren.service.MemberService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/member")
public class MemberController {
    @Reference
    MemberService memberService;
    @RequestMapping("/wl_add")
    public Result add(@RequestBody String string,Integer[] illnessIds){
        try {
            System.out.println(string);
            JSONObject jsonObject = JSON.parseObject(string);
            JSONObject jsonMember = jsonObject.getJSONObject("member");
            Member member = JSON.toJavaObject(jsonMember, Member.class);
            System.out.println(member);
            JSONObject jsonBody = jsonObject.getJSONObject("body");
            Body body = JSON.toJavaObject(jsonBody, Body.class);
            System.out.println(body);
            JSONObject jsonPsychology = jsonObject.getJSONObject("psychology");
            Psychology psychology = JSON.toJavaObject(jsonPsychology, Psychology.class);
            System.out.println(psychology);
//            memberService.add(checkGroup,checkitemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Member> list = memberService.findAll();
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return memberService.findPage(queryPageBean);
    }
    @RequestMapping("/findIllnessPage")
    public PageResult findIllnessPage(@RequestBody QueryPageBean queryPageBean){
        return memberService.findIllnessPage(queryPageBean);
    }

    @RequestMapping("/findAllIllness")
    public Result findAllIllness(){
        List<Illness> list = memberService.findAllIllness();
        if (list != null && list.size() > 0){
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
        }
        return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
    }
}
